﻿// -----------------------------------------------------------------------
// <copyright file="WeekdayRepository.cs" company="AGR">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace AGR.Data.Repository.Contract
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using AGR.Model;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class WeekdayRepository : AbstractRepository<Weekday>
    {
        public WeekdayRepository()
        {
            base._contextHelper = null;
            base._context = new Context.AGRContext();
        }

        public override IQueryable<Weekday> GetAll()
        {
            var result = (from x in base._context.Weekdays
                          select new Weekday
                          {
                              Id = x.Id,
                              WeekDayName = x.WeekDayName,
                              SortCode = x.SortCode,
                              DateCreated = x.DateCreated,
                              UserCreated = x.UserCreated,
                              DateModified = x.DateModified,
                              UserModified = x.UserModified
                          });

            return result;
        }

        public override void Add(Weekday entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Weekday entityToAdd = new Context.Weekday
            {
                WeekDayName = entity.WeekDayName,
                SortCode = entity.SortCode,
                DateCreated = DateTime.Now,
                UserCreated = entity.UserCreated,
                DateModified = null,
                UserModified = null
            };

            base._context.Weekdays.InsertOnSubmit(entityToAdd);
        }

        public override void Update(Weekday entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Weekday entityToUpdate = base._context.Weekdays.Single(x => x.SortCode.Equals(entity.SortCode));

            entityToUpdate.WeekDayName = entity.WeekDayName;
            entityToUpdate.SortCode = entity.SortCode;
            entityToUpdate.DateModified = DateTime.Now;
            entityToUpdate.UserModified = entity.UserModified;
        }

        public override void Delete(Weekday entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Weekday entityToDelete = base._context.Weekdays.Single(x => x.SortCode.Equals(entity.SortCode));

            base._context.Weekdays.DeleteOnSubmit(entityToDelete);
        }
    }
}
